{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Group Anagrams"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: groupAnagrams"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字母异位词分组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串数组，请你将 <strong>字母异位词</strong> 组合在一起。可以按任意顺序返回结果列表。</p>\n",
    "\n",
    "<p><strong>字母异位词</strong> 是由重新排列源单词的所有字母得到的一个新单词。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> strs = <code>[\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"]</code>\n",
    "<strong>输出: </strong>[[\"bat\"],[\"nat\",\"tan\"],[\"ate\",\"eat\",\"tea\"]]</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> strs = <code>[\"\"]</code>\n",
    "<strong>输出: </strong>[[\"\"]]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> strs = <code>[\"a\"]</code>\n",
    "<strong>输出: </strong>[[\"a\"]]</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= strs.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= strs[i].length &lt;= 100</code></li>\n",
    "\t<li><code>strs[i]</code>&nbsp;仅包含小写字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [group-anagrams](https://leetcode.cn/problems/group-anagrams/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [group-anagrams](https://leetcode.cn/problems/group-anagrams/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]', '[\"\"]', '[\"a\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = {}\n",
    "        q = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101]\n",
    "        for x in strs:\n",
    "            n = 1\n",
    "            for i in x:\n",
    "                n *= q[ord(i)-97]\n",
    "            if n in res:\n",
    "                res[n].append(x)\n",
    "            else:\n",
    "                res[n] = [x]\n",
    "        return list(res.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = {}\n",
    "        for word in strs:\n",
    "            sorted_word = ''.join(sorted(word))\n",
    "            if sorted_word in dic:\n",
    "                dic[sorted_word].append(word)\n",
    "            else:\n",
    "                dic[sorted_word] = [word]\n",
    "        return list(dic.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        key = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97, 101]\n",
    "        a = ord(\"a\")\n",
    "        hasmap = {chr(i): key[i-a] for i in range(a, a+26)}\n",
    "        h2 = {}\n",
    "        for s in strs:\n",
    "            cnt = 1\n",
    "            for i in s:\n",
    "                cnt *= hasmap[i]\n",
    "            if cnt in h2:\n",
    "                h2[cnt].append(s)\n",
    "            else:\n",
    "                h2[cnt] = [s]\n",
    "        return list(h2.values())\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = {}\n",
    "        for s in strs:\n",
    "            w = ''.join(sorted(list(s)))\n",
    "            if w in dic:\n",
    "                dic[w].append(s)\n",
    "            else:\n",
    "                dic[w] = [s]\n",
    "        return [dic[k] for k in dic]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic=dict()\n",
    "        for s in strs:\n",
    "            ss=\"\".join(sorted(s))\n",
    "            dic[ss]=dic.get(ss,[])+[s]\n",
    "        return [vals for vals in dic.values()]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        classes=dict()\n",
    "        for s in strs:\n",
    "            sort_s=''.join(sorted(s))\n",
    "            classes[sort_s]=classes.get(sort_s,[])+[s]\n",
    "        return [vals for vals in classes.values()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        word_map = {}\n",
    "        for word in strs:\n",
    "            key = ''.join(sorted(word))\n",
    "            if word_map.get(key, 0) != 0:\n",
    "                word_map[key].append(word)\n",
    "            else:\n",
    "                word_map[key] = [word]\n",
    "        res = []\n",
    "        for key in word_map.keys():\n",
    "            res.append(word_map[key])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dict_ = collections.defaultdict(list)\n",
    "        for i in strs:\n",
    "            key = ''.join(sorted(i))\n",
    "            dict_[key].append(i)\n",
    "        return [i for i in dict_.values()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "# 编写一种方法，对字符串数组进行排序，将所有变位词组合在一起。变位词是指字母相同，但排列不同的字符串。\n",
    "\n",
    "def calc_hash_key(s: str):\n",
    "    table = {}\n",
    "    for ch in s:\n",
    "        if ch not in table:\n",
    "            table[ch] = 0\n",
    "        table[ch] += 1\n",
    "    key = \"\"\n",
    "    for ch in sorted(table.keys()):\n",
    "        key += ch\n",
    "        key += str(table[ch])\n",
    "    return key\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        cache = {}\n",
    "        for str in strs:\n",
    "            hash_key = calc_hash_key(str)\n",
    "            if hash_key not in cache:\n",
    "                cache[hash_key] = []\n",
    "            cache[hash_key].append(str)\n",
    "        \n",
    "        result = []\n",
    "        for key, value in cache.items():\n",
    "            result.append(value)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        # 简化版\n",
    "        dp = {}\n",
    "        for x in strs:\n",
    "            tmp = ''.join(sorted(x))\n",
    "            try: dp[tmp]\n",
    "            except:dp[tmp] = []\n",
    "            dp[tmp] +=  [x]\n",
    "        \n",
    "        # return dp\n",
    "        \n",
    "        return [m for m in dp.values()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = {}\n",
    "        for word in strs:\n",
    "            k = self.charCount(word)\n",
    "            if k not in res.keys():\n",
    "                res[k] = [word]\n",
    "            else:\n",
    "                res[k].append(word)\n",
    "        \n",
    "        return [res[k] for k in res.keys()]\n",
    "\n",
    "    def charCount(self, string: str) -> str:\n",
    "        tmp = [0] * 26 \n",
    "        for c in string:\n",
    "            tmp[ord(c)-ord('a')] += 1\n",
    "        return ''.join([str(c) for c in tmp])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = defaultdict(list)\n",
    "        # return dic\n",
    "        for x in strs:\n",
    "            # return ''.join(sorted(x))\n",
    "            # ''.join(sorted(x)) 保证了他们的字母相同，但排列可能不同\n",
    "            dic[''.join(sorted(x))].append(x)\n",
    "            # return dic\n",
    "        return list(dic.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dictvalue = collections.defaultdict(list)\n",
    "        for v in strs:\n",
    "            key =  \"\".join(sorted(v))\n",
    "            dictvalue[key].append(v)\n",
    "        return list(dictvalue.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            key = \"\".join(sorted(st))\n",
    "            mp[key].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "\n",
    "    def getStrId(self, s):\n",
    "        cnt = collections.Counter(s)\n",
    "        id = \"\"\n",
    "        for i in range(26):\n",
    "            ch = chr(ord('a') + i)\n",
    "            if ch in cnt:\n",
    "                id += str(cnt[ch]) + ch\n",
    "        return id\n",
    "\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        tab = {}\n",
    "        for s in strs:\n",
    "            id = self.getStrId(s)\n",
    "            arry = tab.get(id, [])\n",
    "            arry.append(s)\n",
    "            tab[id] = arry\n",
    "        ret = []\n",
    "\n",
    "        for k in tab:\n",
    "            ret.append(tab[k])\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = defaultdict(list)\n",
    "        for index in strs:\n",
    "            key = ''.join(sorted(index))\n",
    "            res[key].append(index)\n",
    "        return list(res.values())\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        a = collections.defaultdict(list)\n",
    "        for i in strs:\n",
    "            a[''.join(sorted(i))].append(i)\n",
    "        return list(a.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = {}\n",
    "        for word in strs:\n",
    "            k = self.charCount(word)\n",
    "            if k not in res.keys():\n",
    "                res[k] = [word]\n",
    "            else:\n",
    "                res[k].append(word)\n",
    "        \n",
    "        return [res[k] for k in res.keys()]\n",
    "\n",
    "    def charCount(self, string: str) -> str:\n",
    "        tmp = [0] * 26 \n",
    "        for c in string:\n",
    "            tmp[ord(c)-ord('a')] += 1\n",
    "        return ''.join([str(c) for c in tmp])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        # 初始化一个默认字典，值的类型是列表\n",
    "        anagrams = defaultdict(list)\n",
    "        \n",
    "        # 遍历每个字符串\n",
    "        for s in strs:\n",
    "            # 对字符串的字符进行排序，并转换回字符串格式，用作哈希表的键\n",
    "            sorted_str = ''.join(sorted(s))\n",
    "            \n",
    "            # 将原字符串添加到对应键的列表中\n",
    "            anagrams[sorted_str].append(s)\n",
    "        \n",
    "        # 返回哈希表的所有值，即变位词的组\n",
    "        return list(anagrams.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = {}\n",
    "        for word in strs:\n",
    "            k = self.charCount(word)\n",
    "            if k not in res.keys():\n",
    "                res[k] = [word]\n",
    "            else:\n",
    "                res[k].append(word)\n",
    "        \n",
    "        return [res[k] for k in res.keys()]\n",
    "\n",
    "    def charCount(self, string: str) -> str:\n",
    "        tmp = [0] * 26 \n",
    "        for c in string:\n",
    "            tmp[ord(c)-ord('a')] += 1\n",
    "        return ''.join([str(c) for c in tmp])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = {}\n",
    "        for s in strs:\n",
    "            s_key = tuple(sorted(s))\n",
    "            if s_key not in dic:\n",
    "                dic[s_key] = [s]\n",
    "            else:\n",
    "                dic[s_key].append(s)\n",
    "\n",
    "        return [v for k, v in dic.items()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = {}\n",
    "        for s in strs:\n",
    "            s_key = tuple(sorted(s))\n",
    "            if s_key not in dic:\n",
    "                dic[s_key] = [s]\n",
    "            else:\n",
    "                dic[s_key].append(s)\n",
    "\n",
    "        return [v for k, v in dic.items()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        ans = []\n",
    "        mp = dict()\n",
    "        for word in strs:\n",
    "            t = \"\".join(sorted(word))\n",
    "            if t not in mp:\n",
    "                mp[t] = len(ans)\n",
    "                ans.append([word])\n",
    "            else:\n",
    "                ans[mp[t]].append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        ha = {}\n",
    "        for s in strs:\n",
    "            if str(sorted(s)) not in ha:\n",
    "                ha[str(sorted(s))]= [s]\n",
    "            else:\n",
    "                ha[str(sorted(s))] += [s]\n",
    "        \n",
    "        return list(ha.values())\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import *\n",
    "from heapq import *\n",
    "from sys import *\n",
    "from math import *\n",
    "from itertools import *\n",
    "from functools import *\n",
    "from string import *\n",
    "from bisect import *\n",
    "from graphlib import *\n",
    "from sortedcontainers import *\n",
    "from copy import *\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        g = defaultdict(list)\n",
    "        for x in strs:\n",
    "            g[\"\".join(sorted(x))].append(x)\n",
    "        return list(g.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        str1=[]\n",
    "        lis=[]\n",
    "        zong=[]\n",
    "        for i in range(len(strs)):\n",
    "            a=list(strs[i])\n",
    "            a.sort()\n",
    "            str2=\"\".join(a)\n",
    "            str1.append(str2)\n",
    "        b=set(str1)\n",
    "        for j in b:\n",
    "            for i in range(len(str1)):\n",
    "                if str1[i]==j:\n",
    "                    lis.append(strs[i])\n",
    "            zong.append(lis)\n",
    "            lis=[]\n",
    "        return zong \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        # final_list = []\n",
    "        # for i in range(len(strs)):\n",
    "        #     flag = False\n",
    "        #     for item in final_list:\n",
    "        #         if sorted(item[0]) == sorted(strs[i]):\n",
    "        #             item.append(strs[i])\n",
    "        #             flag = True\n",
    "        #             break\n",
    "        #     if not flag:\n",
    "        #         final_list.append([strs[i]])\n",
    "        # return final_list\n",
    "        temp_list = []\n",
    "        temp_dict = {}\n",
    "        final_list = []\n",
    "        for item in strs:\n",
    "            temp_list.append(''.join(sorted(item)))\n",
    "        for i in range(len(temp_list)):\n",
    "            if temp_list[i] in temp_dict.keys():\n",
    "                final_list[temp_dict[temp_list[i]]].append(strs[i])\n",
    "            else:\n",
    "                temp_dict[temp_list[i]] = len(final_list)\n",
    "                final_list.append([strs[i]])\n",
    "        return final_list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        d=defaultdict(list)\n",
    "        for s in strs:\n",
    "            d[tuple(sorted(s))].append(s)\n",
    "        return list(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        hashset = {}\n",
    "        ret = []\n",
    "        for str in strs:\n",
    "            strset = tuple(sorted(str))\n",
    "            if strset not in hashset:\n",
    "                hashset[strset] = []\n",
    "            hashset[strset].append(str)\n",
    "        return list(hashset.values())\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "\n",
    "        d = defaultdict(list)\n",
    "        for s in strs:\n",
    "            d[str(sorted(s))].append(s)\n",
    "        return [v for v in d.values()]\n",
    "\n",
    "# class Solution:\n",
    "#     def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "#         classes = dict()\n",
    "#         for s in strs:\n",
    "#             sort_s = ''.join(sorted(s))\n",
    "#             classes[sort_s] = classes.get(sort_s, []) + [s]\n",
    "#         return [vals for vals in classes.values()]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        RE=[]\n",
    "        tmp = []\n",
    "        for x in strs:\n",
    "            flag = False\n",
    "            Sx = sorted(x)\n",
    "            if Sx not in tmp:\n",
    "                RE.append([x])\n",
    "                tmp.append(Sx)\n",
    "            else:\n",
    "                idx = tmp.index(Sx)\n",
    "                RE[idx].append(x)\n",
    "        return RE\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        r = defaultdict(lambda:[])\n",
    "        b = ord('a')\n",
    "        def getVal(aStr):\n",
    "            r = [0] * 26\n",
    "            for c in aStr:\n",
    "                i = ord(c) - b\n",
    "                r[i] += 1\n",
    "            for i in range(26):\n",
    "                r[i] = str(r[i])\n",
    "            return '-'.join(r)\n",
    "\n",
    "        for s in strs:\n",
    "            r[getVal(s)].append(s)\n",
    "        ret = []\n",
    "        # print(f\"r {r}\")\n",
    "        for k, v in r.items():\n",
    "            ret.append(v)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "\n",
    "        d = defaultdict(list)\n",
    "        for s in strs:\n",
    "            d[str(sorted(s))].append(s)\n",
    "        return [v for v in d.values()]\n",
    "\n",
    "# class Solution:\n",
    "#     def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "#         classes = dict()\n",
    "#         for s in strs:\n",
    "#             sort_s = ''.join(sorted(s))\n",
    "#             classes[sort_s] = classes.get(sort_s, []) + [s]\n",
    "#         return [vals for vals in classes.values()]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        from collections import Counter\n",
    "        d = {}\n",
    "        for s in strs:\n",
    "            freq = [0] * 26\n",
    "            for c, num in Counter(s).most_common():\n",
    "                freq[ord(c) - ord('a')] = num\n",
    "            key = tuple(freq)\n",
    "            d[key] = d.get(key, []) + [s]\n",
    "        return list(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        r = defaultdict(lambda:[])\n",
    "        b = ord('a')\n",
    "        def getVal(aStr):\n",
    "            r = [0] * 26\n",
    "            for c in aStr:\n",
    "                i = ord(c) - b\n",
    "                r[i] += 1\n",
    "            for i in range(26):\n",
    "                r[i] = str(r[i])\n",
    "            return '-'.join(r)\n",
    "\n",
    "        for s in strs:\n",
    "            r[getVal(s)].append(s)\n",
    "        ret = []\n",
    "        # print(f\"r {r}\")\n",
    "        for k, v in r.items():\n",
    "            ret.append(v)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mem = collections.defaultdict(list)\n",
    "\n",
    "        for string in strs:\n",
    "            counter = collections.Counter(string)\n",
    "            key = []\n",
    "            for k in sorted(counter.keys()):\n",
    "                key.append(k)\n",
    "                key.append(counter[k])\n",
    "            mem[tuple(key)].append(string)\n",
    "        return list(mem.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            counts = [0] * 26\n",
    "            for ch in st:\n",
    "                counts[ord(ch) - ord(\"a\")] += 1\n",
    "            mp[tuple(counts)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = {}\n",
    "        ls_o = []\n",
    "        ls = []\n",
    "        for i in strs:\n",
    "            dic[''.join(sorted(i))] = dic.get(''.join(sorted(i)),[]) + [i]\n",
    "            if ''.join(sorted(i)) not in ls_o:\n",
    "                ls_o.append(''.join(sorted(i)))\n",
    "        for k in ls_o:\n",
    "            ls.append(dic[k])\n",
    "        return ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list) #mp是一个类字典，它的键自行赋值，但是它的值的类型是列表(list)，它的值是一个列表。\n",
    "        for st in strs:\n",
    "            count = [0]*26\n",
    "            for ch in st:\n",
    "                count[ord(ch)-ord(\"a\")] += 1\n",
    "            mp[tuple(count)].append(st)\n",
    "        return list(mp.values())\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            counts = [0] * 26\n",
    "            for ch in st:\n",
    "                counts[ord(ch) - ord(\"a\")] += 1\n",
    "            # 需要将 list 转换成 tuple 才能进行哈希\n",
    "            mp[tuple(counts)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        cnt = defaultdict(list)\n",
    "        for s in strs:\n",
    "            key = sorted(Counter(s).items()).__str__()\n",
    "            # print(key)\n",
    "            cnt[key].append(s)\n",
    "        \n",
    "        return [cnt[e] for e in cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        cnt = defaultdict(list)\n",
    "        for s in strs:\n",
    "            key = sorted(Counter(s).items()).__str__()\n",
    "            print(key)\n",
    "            cnt[key].append(s)\n",
    "        \n",
    "        return [cnt[e] for e in cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        cnt = defaultdict(list)\n",
    "        for s in strs:\n",
    "            key = sorted(Counter(s).items()).__str__()\n",
    "            # print(key)\n",
    "            cnt[key].append(s)\n",
    "        \n",
    "        return [cnt[e] for e in cnt]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        result = []\n",
    "        num_dict = {}\n",
    "        for s in strs:\n",
    "            key = self.sum(s)\n",
    "            if num_dict.get(key) is None:\n",
    "                num_dict[key] = []\n",
    "            num_dict[key].append(s)\n",
    "    \n",
    "        for key in num_dict:\n",
    "            l_s = num_dict[key]\n",
    "            result.append(l_s)\n",
    "\n",
    "        return result\n",
    "\n",
    "    def sum(self, s: str) -> int:\n",
    "        chars = [0] * 26\n",
    "        for c in s:\n",
    "            chars[ord(c) - ord('a')] += 1\n",
    "        return tuple(chars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = dict()\n",
    "        for s in strs:\n",
    "            k = ''.join(sorted(s))\n",
    "            if k in dic:\n",
    "                dic[k].append(s)\n",
    "            else:\n",
    "                dic[k] = [s]\n",
    "        return sorted([ sorted(v) for v in dic.values()], key=lambda x:len(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        l = len(strs)\n",
    "        temp = [([0] * 26) for _ in range(l)]\n",
    "        for i in range(len(strs)):\n",
    "            for j in range(len(strs[i])):\n",
    "                temp[i][ord(strs[i][j])-97] += 1\n",
    "\n",
    "        res = []\n",
    "        for i in range(len(strs)):\n",
    "            is_found = False\n",
    "            for j in range(len(res)):\n",
    "                if len(strs[i]) != len(strs[res[j][0]]):\n",
    "                        continue\n",
    "                for k in range(26):\n",
    "                    is_found = True\n",
    "                    if temp[res[j][0]][k] != temp[i][k]:\n",
    "                        is_found = False\n",
    "                        break\n",
    "                if is_found:\n",
    "                    res[j].append(i)\n",
    "                    break   \n",
    "            if not is_found:\n",
    "                l = [i]\n",
    "                res.append(l)\n",
    "\n",
    "        for i in range(len(res)):\n",
    "            for j in range(len(res[i])):\n",
    "                res[i][j] = strs[res[i][j]]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        # dict_list = {}\n",
    "        # for word in strs:\n",
    "        #     order_str = ''.join(sorted(word))\n",
    "        #     dict_list[order_str] = dict_list.get(order_str,[])\n",
    "        #     dict_list[order_str].append(word)\n",
    "        # return [list(v) for v in dict_list.values()]    \n",
    "        res = collections.defaultdict(list)\n",
    "        for str in strs:\n",
    "            count = [0]*36\n",
    "            for ch in str:\n",
    "                count[ord(ch)-ord('a')] +=1\n",
    "            res[tuple(count)].append(str)\n",
    "        return list(res.values())       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        d=defaultdict(list)\n",
    "        for s in strs:\n",
    "            d[s]=sorted(s)\n",
    "        l=[]\n",
    "        for v in d.values():\n",
    "            if v not in l:\n",
    "                l.append(v)\n",
    "        res=[[] for i in range(len(l))]\n",
    "        for s in strs:\n",
    "            for j in range(len(l)):\n",
    "                if d[s]==l[j]:\n",
    "                    res[j].append(s)\n",
    "                    break\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        ls = []\n",
    "        ls2 = []\n",
    "        ls_part = []\n",
    "        for i in range(len(strs)):\n",
    "            dic = dict()\n",
    "            chars = list(strs[i])\n",
    "            for j in chars:\n",
    "                if j not in dic:\n",
    "                    dic[j] = 1\n",
    "                else:\n",
    "                    dic[j] += 1\n",
    "            if dic not in ls:\n",
    "\n",
    "                ls.append(dic)\n",
    "                ls_part = []\n",
    "                ls_part.append(strs[i])\n",
    "                ls2.append(ls_part)\n",
    "            else:\n",
    "                k = 0\n",
    "                while dic != ls[k]:\n",
    "                    k += 1\n",
    "                ls2[k].append(strs[i])\n",
    "        return ls2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        countlist=[]\n",
    "        res=[]\n",
    "        for i in range(len(strs)):\n",
    "            count=collections.Counter()\n",
    "            count.update(strs[i])\n",
    "\n",
    "            if count not in countlist:\n",
    "                countlist.append(count)\n",
    "                res.append([strs[i]])\n",
    "            else:\n",
    "                for j in range(len(countlist)):\n",
    "                    if count==countlist[j]:\n",
    "                        res[j].append(strs[i])\n",
    "                        break            \n",
    "            \n",
    "        #print(countlist)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        from collections import Counter\n",
    "        l = []\n",
    "        res = []\n",
    "        for i in strs:\n",
    "            tem = Counter(i)\n",
    "            flag = 1\n",
    "            for j in range(len(l)):\n",
    "                if tem == l[j]:\n",
    "                    res[j].append(i)\n",
    "                    flag = 0\n",
    "                    break\n",
    "            if flag:\n",
    "                res.append([i])\n",
    "                l.append(tem)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        def word2arr(word):\n",
    "            temp = [ 0 for i in range(26) ]\n",
    "            for c in word:\n",
    "                temp[ord(c)-97] += 1\n",
    "            return temp \n",
    "        \n",
    "        cash = []\n",
    "        ans = []\n",
    "        for word in strs:\n",
    "            cur_word_arr = word2arr(word)\n",
    "            flag = False\n",
    "            #\n",
    "            for idx,prev_word_arr in enumerate(cash):\n",
    "                if cur_word_arr == prev_word_arr:\n",
    "                    ans[idx].append(word)\n",
    "                    flag = True\n",
    "            if not flag:\n",
    "                ans.append([word])\n",
    "                cash.append(cur_word_arr)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        \n",
    "        def str_to_dict_to_str(str2):\n",
    "            str_dict = dict()\n",
    "            for c in str2:\n",
    "                if c in str_dict.keys():\n",
    "                    str_dict[c] = str_dict[c] + 1\n",
    "                else:\n",
    "                    str_dict[c] = 1\n",
    "            \n",
    "            str_dict_list = list(str_dict.items())\n",
    "            print(str_dict_list)\n",
    "            str_dict_list.sort()\n",
    "            print(str_dict_list)\n",
    "            str_dict_list_str = str(str_dict_list)\n",
    "            return str_dict_list_str\n",
    "        \n",
    "        anagram_dict = dict()\n",
    "        for str1 in strs:\n",
    "            key_str = str_to_dict_to_str(str1)\n",
    "            if key_str in anagram_dict.keys():\n",
    "                anagram_dict[key_str].append(str1)\n",
    "            else:\n",
    "                anagram_dict[key_str] = list()\n",
    "                anagram_dict[key_str].append(str1)\n",
    "                \n",
    "        \n",
    "        print(anagram_dict)\n",
    "\n",
    "        anagram_list = list()\n",
    "        for keys, values in anagram_dict.items():\n",
    "            anagram_list.append(values)\n",
    "        \n",
    "        print(anagram_list)\n",
    "\n",
    "        return anagram_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        groups = []\n",
    "        words = []\n",
    "        for word in strs:\n",
    "            word_dict = {}\n",
    "            for c in word:\n",
    "                word_dict.setdefault(c, 0)\n",
    "                word_dict[c] += 1\n",
    "            has_anagram = False\n",
    "            for i in range(len(groups)):\n",
    "                if word_dict == groups[i]:\n",
    "                    words[i].append(word)\n",
    "                    has_anagram = True\n",
    "            if not has_anagram:\n",
    "                groups.append(word_dict)\n",
    "                words.append([word])\n",
    "        return words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        from collections import Counter\n",
    "        hashDict=dict()\n",
    "        key=[]\n",
    "        for i in strs:\n",
    "            count=Counter(i)            \n",
    "            if(count not in key):\n",
    "                key.append(count)\n",
    "                hashDict[len(key)-1]=[i]\n",
    "            else:\n",
    "                index=key.index(count)\n",
    "                hashDict[index].append(i)\n",
    "        # print((hashDict.values()))\n",
    "        return list(hashDict.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = []\n",
    "        new_strs = []\n",
    "        for i in range(len(strs)):\n",
    "            new_strs.append([i, ''.join(sorted(strs[i]))])\n",
    "        dic = defaultdict(list)\n",
    "        for i in range(len(strs)):\n",
    "            dic[new_strs[i][1]].append(strs[new_strs[i][0]])\n",
    "        for i in dic:\n",
    "            res.append(dic[i])\n",
    "        return res\n",
    "            \n",
    "            \n",
    "                    \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic={}\n",
    "        re=[]\n",
    "        for i in range(len(strs)):\n",
    "            tt=[0 for _ in range(26)]\n",
    "            for j in strs[i]:\n",
    "                tt[ord(j)-ord('a')]+=1\n",
    "                #ha |= (1<<(ord(j)-ord('a')))\n",
    "            ha=tuple(tt)\n",
    "            if ha in dic:\n",
    "                dic[ha].append(i)\n",
    "            else:\n",
    "                dic[ha]=[i]\n",
    "        for i in dic:\n",
    "            t=[]\n",
    "            for j in dic[i]:\n",
    "                t.append(strs[j])\n",
    "            re.append(t)\n",
    "        return re\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        result_dict = {}\n",
    "        num = 0\n",
    "        # 遍历所有的值，排序后生成dict\n",
    "        for i in strs:\n",
    "            tmp = [j for j in i if j !='' ]\n",
    "            res = sorted(tmp)\n",
    "            word_str = ''\n",
    "            result_dict[num] = word_str.join(res)\n",
    "            num += 1\n",
    "        # 查找dict中相同的值，获取key\n",
    "        same_dict = {}\n",
    "        for key, value in result_dict.items():\n",
    "            if value not in same_dict:\n",
    "                same_dict[value] = [key]\n",
    "            else:\n",
    "                same_dict[value].append(key)\n",
    "        # 输入最终结果\n",
    "        result_list = []\n",
    "        for key, value in same_dict.items():\n",
    "            result_small_list = []\n",
    "            for i in value:\n",
    "                result_small_list.append(strs[i])\n",
    "            result_list.append(result_small_list)\n",
    "        return result_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        d = defaultdict(list)\n",
    "        for i, x in enumerate(strs):\n",
    "            t = list(x)\n",
    "            t.sort()\n",
    "            d[''.join(t)].append(i)\n",
    "        res = []\n",
    "        for h in d.values():\n",
    "            res.append([strs[y] for y in h])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic = dict()\n",
    "        for s in strs:\n",
    "            k = tuple( sorted(s) )\n",
    "            if k in dic:\n",
    "                dic[k].append(s)\n",
    "            else:\n",
    "                dic[k] = [s]\n",
    "        return sorted([ sorted(v) for v in dic.values()], key=lambda x:len(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        ans_dict = dict()\n",
    "        key_idx = 0\n",
    "        ans_l = []\n",
    "        for per_e in strs:\n",
    "            is_find = False\n",
    "            distribute = Counter(per_e)\n",
    "            for k, v in ans_dict.items():\n",
    "                if distribute == v:\n",
    "                    ans_l[k].append(per_e)\n",
    "                    is_find = True\n",
    "                    break\n",
    "            if not is_find:\n",
    "                ans_dict[key_idx] = distribute\n",
    "                ans_l.append([per_e])\n",
    "                key_idx += 1\n",
    "        return ans_l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        memo = defaultdict(list)\n",
    "        for word in strs:\n",
    "            test = list(word)\n",
    "            test.sort()\n",
    "            k = [0] * 26\n",
    "            for ch in test:\n",
    "                k[ord(ch) - 97] += 1\n",
    "            s = ''.join(str(k))\n",
    "            memo[s].append(word)\n",
    "        print(memo)\n",
    "\n",
    "        ans = []\n",
    "\n",
    "        for key in memo.keys():\n",
    "            ans.append(memo[key])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        ans = []\n",
    "        n = len(strs)\n",
    "        tmp =[]\n",
    "        for each in strs:\n",
    "            tmp.append(dict(collections.Counter(each)))\n",
    "\n",
    "        ans2 = []\n",
    "        for i in range(n):\n",
    "            if ans == []:\n",
    "                ans.append([strs[i]])\n",
    "                ans2.append(i)#记录0号索引\n",
    "            else:\n",
    "                flag = 0\n",
    "                for j in range(len(ans)):\n",
    "                    if tmp[i] == tmp[ans2[j]]:\n",
    "                        ans[j].append(strs[i])\n",
    "                        flag = 1\n",
    "                        break\n",
    "                if flag == 0:\n",
    "                    #说明没找到\n",
    "                    ans.append([strs[i]])\n",
    "                    ans2.append(i)\n",
    "\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        # 直接用数组 char[26] 作为字典的key\n",
    "        dic = {}\n",
    "        n = len(strs)\n",
    "        for i in range(n):\n",
    "            s = strs[i]\n",
    "            vec = [0 for i in range(26)]\n",
    "            for c in s:\n",
    "                vec[ord(c) - 97] += 1\n",
    "            k = tuple(vec)\n",
    "            if k in dic:\n",
    "                dic[k].append(i)\n",
    "            else:\n",
    "                dic[k] = [i]\n",
    "        r = []\n",
    "        for k in dic.keys():\n",
    "            t = []\n",
    "            for i in dic[k]:\n",
    "                t.append(strs[i])\n",
    "            r.append(t)\n",
    "            \n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp={}\n",
    "        for st in strs:\n",
    "            count=[0]*26\n",
    "            for ch in st:\n",
    "                count[ord(ch)-ord('a')]+=1\n",
    "            if tuple(count)  in mp:\n",
    "                mp[tuple(count)].append(st)\n",
    "            else:\n",
    "                mp[tuple(count)]=[st]\n",
    "        return list(mp.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        temp = sorted([[sorted(strs[i]), i] for i in range(len(strs))], key = lambda x : x[0])\n",
    "        ans = []\n",
    "        temp2 = [strs[temp[0][1]]]\n",
    "        for i in range(1, len(temp)):\n",
    "            if temp[i][0] == temp[i-1][0]:\n",
    "                temp2.append(strs[temp[i][1]])\n",
    "            else:\n",
    "                ans.append(temp2)\n",
    "                temp2 = [strs[temp[i][1]]]\n",
    "        ans.append(temp2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        n = len(strs)\n",
    "        if n == 1:\n",
    "            return [strs]\n",
    "\n",
    "        s_dict = {}\n",
    "        for s in strs[0]:\n",
    "            if s in s_dict:\n",
    "                s_dict[s] += 1\n",
    "            else:\n",
    "                s_dict[s] = 1\n",
    "\n",
    "        res = [[[strs[0]], s_dict]]\n",
    "        for i in range(1, n):\n",
    "            s_dict = {}\n",
    "            for s in strs[i]:\n",
    "                if s in s_dict:\n",
    "                    s_dict[s] += 1\n",
    "                else:\n",
    "                    s_dict[s] = 1\n",
    "\n",
    "            flag = 0\n",
    "            for j in range(len(res)):\n",
    "                if res[j][1] == s_dict:\n",
    "                    res[j][0].append(strs[i])\n",
    "                    flag = 1\n",
    "                    break\n",
    "            \n",
    "            if flag == 0:\n",
    "                res.append([[strs[i]], s_dict])\n",
    "\n",
    "        for i in range(len(res)):\n",
    "            res[i] = res[i][0]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        d = defaultdict(list)\n",
    "        n = len(strs)\n",
    "        for i in range(n):\n",
    "            k = [0] * 26\n",
    "            for c in strs[i]:\n",
    "                k[ord(c) - ord('a')] += 1\n",
    "            d[tuple(k)].append(i)\n",
    "        ans = []\n",
    "        for li in d.values():\n",
    "            ans.append([strs[x] for x in li])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        ans =[[strs[0]]]\n",
    "        base = [Counter(strs[0])]\n",
    "        lens = [len(strs[0])]\n",
    "        for i in range(1, len(strs)):\n",
    "            n = len(strs[i])\n",
    "            new_cnt = Counter(strs[i])\n",
    "            match = False\n",
    "            for j in range(len(base)):\n",
    "                if n != lens[j]:\n",
    "                    continue                \n",
    "                if new_cnt == base[j]:\n",
    "                    ans[j].append(strs[i])\n",
    "                    match = True\n",
    "                    break\n",
    "            if not match:\n",
    "                base.append(new_cnt)\n",
    "                ans.append([strs[i]])\n",
    "                lens.append(n)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        arr=[]\n",
    "        num=list(range(len(strs)))\n",
    "        for i in strs:\n",
    "            arr.append(sorted(list(i)))\n",
    "        t=sorted(zip(arr,num))\n",
    "        print(t)\n",
    "        ans=[]\n",
    "        for i in range(len(t)):\n",
    "            if i==0 or t[i][0]!=t[i-1][0]:\n",
    "                ans.append([strs[t[i][1]]])\n",
    "            else:\n",
    "                ans[-1]+=[strs[t[i][1]]]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef groupAnagrams(self, strs):\n",
    "\t\tgrp = {}\n",
    "\t\tfor s in strs:\n",
    "\t\t\tkey = self.calc(s)\n",
    "\t\t\tif key in grp:\n",
    "\t\t\t\tgrp[key].append(s)\n",
    "\t\t\telse:\n",
    "\t\t\t\tgrp[key] = [s]\n",
    "\t\treturn list(grp.values())\n",
    "\t\n",
    "\tdef calc(self, s):\n",
    "\t\tcnt = [0] * 26\n",
    "\t\tfor char in s:\n",
    "\t\t\tcnt[ord(char) - 97] += 1\n",
    "\t\treturn tuple(cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            counts = [0] * 26\n",
    "            for ch in st:\n",
    "                counts[ord(ch) - ord(\"a\")] += 1\n",
    "            mp[tuple(counts)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            count = [0] * 26\n",
    "            for c in st: \n",
    "                count[ord(c) - ord('a')] += 1\n",
    "            mp[tuple(count)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        n = len(strs)\n",
    "        string_set = dict()\n",
    "        for s in range(n):\n",
    "            cs = [0 for i in range(26)]\n",
    "            for c in strs[s]:\n",
    "                cs[ord(c)-ord('a')] += 1\n",
    "            cst = tuple(cs)\n",
    "            if cst not in string_set:\n",
    "                string_set[cst] = []\n",
    "            string_set[cst].append(strs[s])\n",
    "        ans = [s for _, s in string_set.items()]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            counts = [0] * 26\n",
    "            for ch in st:\n",
    "                counts[ord(ch) - ord(\"a\")] += 1\n",
    "            # 需要将 list 转换成 tuple 才能进行哈希\n",
    "            mp[tuple(counts)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            counts = [0] * 26\n",
    "            for ch in st:\n",
    "                counts[ord(ch) - ord(\"a\")] += 1\n",
    "            # 需要将 list 转换成 tuple 才能进行哈希\n",
    "            mp[tuple(counts)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            counts = [0] * 26\n",
    "            for ch in st:\n",
    "                counts[ord(ch) - ord(\"a\")] += 1\n",
    "            # 需要将 list 转换成 tuple 才能进行哈希\n",
    "            mp[tuple(counts)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dic=defaultdict(list)\n",
    "        for word in strs:\n",
    "            cnt=[0]*26\n",
    "            for ch in word:\n",
    "                cnt[ord(ch)-ord('a')]+=1\n",
    "            dic[tuple(cnt)].append(word)\n",
    "        return [value for key,value in dic.items()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        nums = collections.defaultdict(list)\n",
    "        for st in strs:\n",
    "            ch = [0] * 26\n",
    "            for s in st:\n",
    "                ch[ord(s)-ord('a')] += 1\n",
    "            nums[tuple(ch)].append(st)\n",
    "        return list(nums.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        def getAnagram(s):\n",
    "            count = [0]*26\n",
    "            for c in s:\n",
    "                count[ord(c) - ord('a')]+=1\n",
    "            return tuple(count)\n",
    "        \n",
    "        memo = defaultdict(list)\n",
    "        for s in strs:\n",
    "            memo[getAnagram(s)].append(s)\n",
    "        return list(memo.values())\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        # 26 个质数\n",
    "        primes = [2, 3, 5, 7, 11,\n",
    "                  13, 17, 19, 23, 29,\n",
    "                  31, 37, 41, 43, 47,\n",
    "                  53, 59, 61, 67, 71,\n",
    "                  73, 79, 83, 89, 97,\n",
    "                  101]\n",
    "\n",
    "        # hash_arr 中存放的是与 res 对应的 hash 值\n",
    "        hash_arr = []\n",
    "        res = []\n",
    "\n",
    "        for s in strs:\n",
    "            hash_val = 1\n",
    "            for alpha in s:\n",
    "                hash_val *= primes[ord(alpha) - ord('a')]\n",
    "            index = 0\n",
    "            while index < len(hash_arr):\n",
    "                if hash_arr[index] == hash_val:\n",
    "                    res[index].append(s)\n",
    "                    break\n",
    "                index += 1\n",
    "            if index == len(hash_arr):\n",
    "                hash_arr.append(hash_val)\n",
    "                res.append([s])\n",
    "        return res\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    strs = [\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"]\n",
    "    solution = Solution()\n",
    "    result = solution.groupAnagrams(strs)\n",
    "    print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        dict_ = {}\n",
    "        for str in strs:\n",
    "            key = ''.join(sorted(str))\n",
    "            if key in dict_:\n",
    "                dict_[key].append(str)\n",
    "            else:\n",
    "                dict_[key] = [str]\n",
    "\n",
    "        return list(dict_.values()) #In python 3, you have to return list(d.values) since d.values only returns the view\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        hashTable = {}\n",
    "        result = []\n",
    "        \n",
    "        for s in strs:\n",
    "            temp = list(s)\n",
    "            list.sort(temp)\n",
    "            key = \"\".join(temp)\n",
    "            if key not in hashTable:\n",
    "                hashTable[key] = []\n",
    "            hashTable[key].append(s)\n",
    "        \n",
    "        for _, item in hashTable.items():\n",
    "            result.append(item)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        judge = []\n",
    "        flag = -1\n",
    "        for i in range(len(strs)):\n",
    "            b = ''.join(sorted(strs[i]))\n",
    "            for j in range(len(res)):\n",
    "                if b in judge[j]:\n",
    "                    flag = j\n",
    "            if flag>=0:\n",
    "                res[flag] = res[flag]+[strs[i]]\n",
    "            else:\n",
    "                res.append([strs[i]])\n",
    "                judge.append([b])\n",
    "            flag = -1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        dict_ = {}\n",
    "        res = []\n",
    "        for i in range(len(strs)):\n",
    "            mid = str(sorted(strs[i]))\n",
    "            if mid in dict_.keys():\n",
    "                dict_[mid].append(strs[i])\n",
    "            else:\n",
    "                dict_[mid] = []\n",
    "                dict_[mid].append(strs[i])\n",
    "        for item in dict_.keys():\n",
    "            res.append(dict_[item])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        dic = {}\n",
    "        for i in range(len(strs)):\n",
    "            tmp = sorted(strs[i])\n",
    "            tmp = tuple(tmp)\n",
    "            if dic.get(tmp, 0) == 0:\n",
    "                dic[tmp] = [strs[i]]\n",
    "            else:\n",
    "                dic[tmp].append(strs[i])\n",
    "        res = []\n",
    "        for i in dic.keys():\n",
    "            res.append(dic[i])\n",
    "        return res\n",
    "        \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def groupAnagrams(self, strs):\n",
    "        ans = collections.defaultdict(list)\n",
    "        for s in strs:\n",
    "            ans[tuple(sorted(s))].append(s)\n",
    "        return list(ans.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def groupAnagrams(self, strs):\n",
    "        import collections\n",
    "        ans = collections.defaultdict(list)\n",
    "        for s in strs:\n",
    "            ans[tuple(sorted(s))].append(s)\n",
    "        return list(ans.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        a=[]\n",
    "        c=[]\n",
    "        \n",
    "        for i in strs:\n",
    "            b=sorted(list(i))\n",
    "            if b not in a:\n",
    "                a.append(b)\n",
    "                c.append([])\n",
    "                c[-1].append(i)\n",
    "            else:            \n",
    "                c[a.index(b)].append(i)\n",
    "                \n",
    "        return c\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import itertools\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        return [list(g) for _, g in itertools.groupby(sorted(strs, key=sorted), sorted)]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        ans = collections.defaultdict(list)\n",
    "        for s in strs:\n",
    "            count = [0] * 26\n",
    "            for c in s:\n",
    "                count[ord(c) - ord('a')] += 1\n",
    "            ans[tuple(count)].append(s)\n",
    "        return list(ans.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        rel=[]\n",
    "        rel_dict=[]\n",
    "        for item in strs:\n",
    "            map_dict={}\n",
    "            for c in item:\n",
    "                if(c in map_dict):\n",
    "                    map_dict[c]=map_dict[c]+1\n",
    "                else:\n",
    "                    map_dict[c]=1\n",
    "            if(map_dict in rel_dict):\n",
    "                rel[rel_dict.index(map_dict)].append(item)\n",
    "            else:\n",
    "                rel_dict.append(map_dict)\n",
    "                rel.append([item])\n",
    "        return rel\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"        \n",
    "        def createDic(s):\n",
    "            dic = {}\n",
    "            for c in s:\n",
    "                dic[c] = dic.get(c, 0) + 1\n",
    "            return dic\n",
    "                \n",
    "        result = {}\n",
    "        dics = {}\n",
    "        for s in strs:\n",
    "            dic = createDic(s)\n",
    "            finded = False\n",
    "            for k, v in dics.items():\n",
    "                if dic == v:\n",
    "                    finded = True\n",
    "                    result[k].append(s)\n",
    "                    break\n",
    "            \n",
    "            if not finded:\n",
    "                dics[s] = dic\n",
    "                result[s] = [s]\n",
    "        return list(result.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"        \n",
    "        def createDic(s):\n",
    "            dic = {'len': len(s)}\n",
    "            for c in s:\n",
    "                dic[c] = dic.get(c, 0) + 1\n",
    "            return dic\n",
    "                \n",
    "        result = {}\n",
    "        dics = {}\n",
    "        for s in strs:\n",
    "            dic = createDic(s)\n",
    "            finded = False\n",
    "            for k, v in dics.items():\n",
    "                if dic == v:\n",
    "                    finded = True\n",
    "                    result[k].append(s)\n",
    "                    break\n",
    "            \n",
    "            if not finded:\n",
    "                dics[s] = dic\n",
    "                result[s] = [s]\n",
    "        return list(result.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        def isSame(dic1, dic2):\n",
    "            if dic1['len'] != dic2['len']:\n",
    "                return False\n",
    "            for k, v in dic1.items():\n",
    "                if k not in dic2:\n",
    "                    return False\n",
    "                if dic1[k] != dic2[k]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        def createDic(s):\n",
    "            dic = {'len': len(s)}\n",
    "            for c in s:\n",
    "                dic[c] = dic.get(c, 0) + 1\n",
    "            return dic\n",
    "                \n",
    "        result = {}\n",
    "        dics = {}\n",
    "        for s in strs:\n",
    "            dic = createDic(s)\n",
    "            finded = False\n",
    "            for k, v in dics.items():\n",
    "                if isSame(dic, v):\n",
    "                    finded = True\n",
    "                    result[k].append(s)\n",
    "                    break\n",
    "            \n",
    "            if not finded:\n",
    "                dics[s] = dic\n",
    "                result[s] = [s]\n",
    "        return list(result.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: 'List[str]') -> 'List[List[str]]':\n",
    "        sortStrs = [sorted(strs[i]) for i in range(len(strs))]\n",
    "\n",
    "        result_index_dict = {}\n",
    "        for i in range(len(sortStrs)):\n",
    "            if tuple(sortStrs[i]) not in result_index_dict.keys():\n",
    "                result_index_dict[tuple(sortStrs[i])] = [i]\n",
    "            else:\n",
    "                result_index_dict[tuple(sortStrs[i])].extend([i])\n",
    "\n",
    "        result = []\n",
    "        for key, val in result_index_dict.items():\n",
    "            curItem = [strs[index] for index in val]\n",
    "            result.append(curItem)\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        key,value = [],[]\n",
    "        from collections import Counter\n",
    "        for i in strs:\n",
    "            if Counter(i) not in key:\n",
    "                key.append(Counter(i))\n",
    "                value.append([i])\n",
    "            else:\n",
    "                value[key.index(Counter(i))].append(i)\n",
    "        return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        self.s={}\n",
    "        self.res=[]\n",
    "        for st in strs:\n",
    "            index=self.findgroup(st)\n",
    "            if index != -1:\n",
    "                self.res[index].append(st)\n",
    "            else:\n",
    "                continue\n",
    "        return self.res\n",
    "    def findgroup(self,now):\n",
    "        counter=Counter(now)\n",
    "        tc=tuple(sorted(counter.items()))\n",
    "        if tc in self.s:\n",
    "            return self.s[tc]\n",
    "        else:\n",
    "            self.res.append([now])\n",
    "            self.s[tc]=len(self.res)-1\n",
    "            return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = []\n",
    "        dic = {}\n",
    "        for s in strs:\n",
    "            keys = \"\".join(sorted(s))\n",
    "            if keys not in dic:\n",
    "                dic[keys] = [s]\n",
    "            else:\n",
    "                dic[keys].append(s)\n",
    "        return list(dic.values())\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "\n",
    "        dict = {}\n",
    "        for s in strs:\n",
    "            if dict.get(len(s)) is not None:\n",
    "                dict[len(s)].append(s)\n",
    "            else:\n",
    "                dict[len(s)] = [s]\n",
    "\n",
    "        strs = [dict.get(key) for key in dict]\n",
    "        result = []\n",
    "        for s in strs:\n",
    "            z_result = []\n",
    "            for s1 in s:\n",
    "                if z_result:\n",
    "                    flag = True\n",
    "                    for z in z_result:\n",
    "                        if self.is_like(s1, z[0]):\n",
    "                            z.append(s1)\n",
    "                            flag = False\n",
    "                            break\n",
    "                    if flag:\n",
    "                        z_result.append([s1])\n",
    "                else:\n",
    "                    z_result.append([s1])\n",
    "\n",
    "            result.extend(z_result)\n",
    "        return result\n",
    "\n",
    "    def is_like(self, s1, s2):\n",
    "        \"\"\"判断s1与s2是否是字母异位词\"\"\"\n",
    "        if sorted(s1) == sorted(s2):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\r\n",
    "\r\n",
    "        x=[]\r\n",
    "        for s in strs:\r\n",
    "            if x is None:\r\n",
    "                x.append([s])\r\n",
    "                continue\r\n",
    "            find=False\r\n",
    "            for xx in x:\r\n",
    "                if len(xx[0])!=len(s):\r\n",
    "                    continue\r\n",
    "                xxx=set(xx[0])\r\n",
    "                sset=set(s)\r\n",
    "                if sset==xxx:\r\n",
    "                # if sset==set([]) and xxx-sset==set([]):\r\n",
    "                    match=True\r\n",
    "                    for temp in sset:\r\n",
    "                        num=0\r\n",
    "                        for i in xx[0]:\r\n",
    "                            if i==temp:num+=1\r\n",
    "                        for i in s:\r\n",
    "                            if i==temp:num-=1\r\n",
    "                        if num!=0:\r\n",
    "                            match=False\r\n",
    "                            break\r\n",
    "                    if not match:\r\n",
    "                        continue\r\n",
    "                    xx.append(s)\r\n",
    "                    find=True\r\n",
    "                    break\r\n",
    "            if not find:\r\n",
    "                x.append([s])\r\n",
    "        return x\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        res = []\n",
    "\n",
    "        for s in strs:\n",
    "            flag = True\n",
    "            for xs in res:\n",
    "                if len(s) == len(xs[0]) and sorted(s) == sorted(xs[0]):\n",
    "                    xs.append(s)\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                res.append([s])\n",
    "        # print(res)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs):\n",
    "        ans = {}\n",
    "        for s in strs:\n",
    "            _s = ''.join(sorted(s))\n",
    "            ans[_s] = ans.get(_s, []) + [s]\n",
    "        \n",
    "        return list(ans.values())\n",
    "\n",
    "\n",
    "    # def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "    #     ans = collections.defaultdict(list)\n",
    "    #     for s in strs:\n",
    "    #         counter = [0] * 26\n",
    "    #         for c in s:\n",
    "    #             counter[ord(c) - ord('a')] += 1\n",
    "    #         # 注意这里的counter是对象，因此在修改后之前的赋值也会随之改变！！！\n",
    "    #         ans[tuple(counter)].append(s)\n",
    "        \n",
    "    #     return list(ans.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        if not strs:\n",
    "            return []\n",
    "        res = []\n",
    "        dic = {}\n",
    "        for s in strs:\n",
    "            keys = \"\".join(sorted(s))\n",
    "            if keys not in dic:\n",
    "                dic[keys] = [s]\n",
    "            else:\n",
    "                dic[keys].append(s)\n",
    "        return list(dic.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "\n",
    "        ans = []\n",
    "        uniq = {}\n",
    "\n",
    "        for i in strs:\n",
    "            temp = \"\".join(sorted(list(i)))\n",
    "            if temp not in uniq:\n",
    "                ans.append([i])\n",
    "                uniq.update({temp:len(ans)-1})\n",
    "            else:\n",
    "                ans[uniq[temp]].append(i)\n",
    "        \n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res=[]\n",
    "        a=0\n",
    "        for i in strs:\n",
    "            \n",
    "            for j in res:\n",
    "                if len(i)!=len(j[0]):\n",
    "                    continue\n",
    "                if sorted(i)==sorted(j[0]):\n",
    "                    j.append(i)\n",
    "                    a=1\n",
    "                    break\n",
    "            if a==1:\n",
    "                a=0\n",
    "                continue\n",
    "            else:\n",
    "                res.append([i])\n",
    "           \n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        if len(strs)==1:\n",
    "            return [strs]\n",
    "        result = []\n",
    "        while strs:\n",
    "            current = [strs[0]]\n",
    "            item = sorted(strs[0])\n",
    "            for st in strs[1:]:\n",
    "                if len(st)!=len(strs[0]):\n",
    "                    continue\n",
    "                if sorted(st)==item:\n",
    "                    current.append(st)\n",
    "            for st in current:\n",
    "                strs.remove(st)\n",
    "            result.append(current)\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        def yiwei(str1,str2):\n",
    "            if len(str1)==len(str2):\n",
    "                if set(str1)==set(str2):\n",
    "                    for i in set(str1):\n",
    "                        if str1.count(i)==str2.count(i):\n",
    "                            continue\n",
    "                        else:\n",
    "                            return False\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "            else:\n",
    "                return False\n",
    "        shuchu=[]\n",
    "        if len(strs)==0:\n",
    "            return shuchu\n",
    "        elif len(strs)==1:\n",
    "            shuchu.append(strs)\n",
    "            return shuchu\n",
    "        else:\n",
    "            while strs!=[]:\n",
    "                liebiao=[strs[0]]\n",
    "                del strs[0]\n",
    "                for i in strs:\n",
    "                    if yiwei(liebiao[0],i):\n",
    "                        liebiao.append(i)\n",
    "                shuchu.append(liebiao)\n",
    "                if len(liebiao)>1:\n",
    "                    for i in liebiao[1:]:\n",
    "                        strs.remove(i)\n",
    "            return shuchu\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        if len(strs) <= 1:\n",
    "            return [strs]\n",
    "        \n",
    "        def judge(s1, s2):\n",
    "            if len(s1) != len(s2):\n",
    "                return False\n",
    "            return sorted(s1) == sorted(s2)\n",
    "\n",
    "        result = []\n",
    "        tmpResult = []\n",
    "        while (strs):\n",
    "            s1 = strs.pop(0)\n",
    "            tmpResult.append(s1)\n",
    "            tmpStrs = strs[:]\n",
    "            for i in range(len(tmpStrs)):\n",
    "                if judge(s1, tmpStrs[i]):\n",
    "                    tmpResult.append(tmpStrs[i])\n",
    "                    strs.remove(tmpStrs[i])\n",
    "            result.append(tmpResult[:])\n",
    "            tmpResult = []\n",
    "        return result\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        #--my solution 1--6448ms--5.01%--17.7MB--99%--\n",
    "        results = []\n",
    "        s_list = []\n",
    "        s_list.append(strs[0])\n",
    "        results.append(s_list)\n",
    "        tag = 1\n",
    "        for s in strs[1:]:\n",
    "            for i in range(0,len(results)):\n",
    "                tag = 1\n",
    "                temp = results[i][0]\n",
    "                s_temp = s\n",
    "                if len(temp) != len(s_temp):\n",
    "                    tag = 0\n",
    "                else:\n",
    "                    for ss in temp:\n",
    "                        if ss not in s_temp:\n",
    "                            tag = 0\n",
    "                            break\n",
    "                        else:\n",
    "                            s_temp = s_temp.replace(ss,\"\",1)   #防止[dddddg,dggggg],只删除一个\n",
    "                #print(tag)\n",
    "                if tag == 1:\n",
    "                    results[i].append(s)\n",
    "                    break\n",
    "            if tag == 0:\n",
    "                s_list = []\n",
    "                s_list.append(s)\n",
    "                results.append(s_list)\n",
    "            tag = 1\n",
    "        return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        dict = {}\n",
    "        for s in strs:\n",
    "            s_ = \"\".join(sorted(s))\n",
    "            if s_ in dict:\n",
    "                dict[s_].append(s)\n",
    "            else:\n",
    "                dict[s_] = [s]\n",
    "        return list(dict.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        answer = []\n",
    "        i = 0\n",
    "        while i < len(strs):\n",
    "            last = [strs[i]]\n",
    "            j = i+1\n",
    "            while i+1 <= j < len(strs):\n",
    "                if len(strs[i]) == len(strs[j]):\n",
    "                    for m in strs[j]:\n",
    "                        if m not in strs[i]:\n",
    "                            break\n",
    "                        elif strs[j].count(m) != strs[i].count(m):\n",
    "                            break\n",
    "                    else:\n",
    "                        last.append(strs[j])\n",
    "                        strs.pop(j)\n",
    "                        j -= 1\n",
    "                j += 1\n",
    "            answer.append(last)\n",
    "            i += 1\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res=[]\n",
    "        while strs:\n",
    "            tmp_a=strs.pop(0)\n",
    "            tmp_list=[tmp_a]\n",
    "            for i in strs:\n",
    "                if len(tmp_a)==len(i):\n",
    "                    if sorted(tmp_a)==sorted(i):\n",
    "                        tmp_list.append(i)\n",
    "            res.append(tmp_list) \n",
    "            strs=[i for i in strs if i not in tmp_list]\n",
    "        return res           \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        list0 = strs[:]\n",
    "        res = []\n",
    "        def special_dict(strs):\n",
    "            dict0 = set(strs)\n",
    "            dict1 = [letter+str(strs.count(letter)) for letter in dict0]\n",
    "            return set(dict1)\n",
    "\n",
    "        while list0:\n",
    "            tmp_list = []\n",
    "            tmp_var =  list0.pop()\n",
    "            tmp_list.append(tmp_var)\n",
    "            for i in range(len(list0)):\n",
    "                if len(list0[i])==len(tmp_var) and set(list0[i])==set(tmp_var) and special_dict(list0[i]) == special_dict(tmp_var):\n",
    "                    tmp_list.append(list0[i])\n",
    "           \n",
    "            for del_var in tmp_list[1:]:\n",
    "                list0.remove(del_var)\n",
    "            res.append(tmp_list)\n",
    "        return(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def groupAnagrams(self, strs):\n",
    "        \"\"\"\n",
    "        :type strs: List[str]\n",
    "        :rtype: List[List[str]]\n",
    "        \"\"\"\n",
    "        dict_map = {}\n",
    "        for str in strs:\n",
    "            dict_str = \"\".join(sorted(str))\n",
    "            if dict_str not in dict_map:\n",
    "                dict_map[dict_str] = [str]\n",
    "            else:\n",
    "                dict_map[dict_str].append(str)\n",
    "\n",
    "        return_list = []\n",
    "        for key in dict_map:\n",
    "            return_list.append(dict_map[key])\n",
    "        return return_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "\n",
    "        mp = defaultdict(list)\n",
    "        strs.sort()\n",
    "\n",
    "        for string in strs:\n",
    "            anagram = \"\".join(sorted(string))\n",
    "            mp[anagram].append(string)\n",
    "\n",
    "        ans = []\n",
    "        \n",
    "        for anagram in mp:\n",
    "            ans.append(mp[anagram])\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        res = {}\n",
    "        for i in strs:\n",
    "            key = tuple(sorted(i))\n",
    "            res[key] = res.get(key, []) + [i]\n",
    "        return list(res.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        items = {}\n",
    "        for word in strs:\n",
    "            key = tuple(sorted(word))\n",
    "            if key in items:\n",
    "                items[key].append(word)\n",
    "            else:\n",
    "                items[key] = [word]\n",
    "        return list(items.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        my_dict = defaultdict(list)\n",
    "        for i in range(len(strs)):\n",
    "            key = self.get_count(strs[i])\n",
    "            my_dict[key].append(strs[i])\n",
    "        \n",
    "        res = []\n",
    "        for key in my_dict: res.append(my_dict[key])\n",
    "        return res\n",
    "\n",
    "    def get_count(self, string):\n",
    "        count = [0] * 26\n",
    "        for ch in string:\n",
    "            count[ord(ch) - ord('a')] += 1\n",
    "        return str(count)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        d = {}\n",
    "        for i, s in enumerate(strs):\n",
    "            s1 = ''.join(sorted(s))\n",
    "            if s1 in d:\n",
    "                va = d[s1]\n",
    "                va.append(i)\n",
    "                d[s1] = va\n",
    "            else:\n",
    "                d[s1] = [i]\n",
    "        l = []\n",
    "        for k, v in d.items():\n",
    "            l_str = []\n",
    "            for m in v:\n",
    "                l_str.append(strs[m])\n",
    "            l.append(l_str)    \n",
    "        return l\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\r\n",
    "        hashtable = dict()\r\n",
    "        for i,str in enumerate(strs):\r\n",
    "            key_str = list(str)\r\n",
    "            key_str.sort()\r\n",
    "            key_str = \"\".join(key_str)\r\n",
    "            if key_str not in hashtable:\r\n",
    "                hashtable[key_str] = [i]\r\n",
    "            else:\r\n",
    "                hashtable[key_str].append(i)\r\n",
    "        \r\n",
    "        result = []\r\n",
    "        for k, v in hashtable.items():\r\n",
    "            result.append([strs[i] for i in v])\r\n",
    "        return result\r\n",
    "        \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        if strs is None or len(strs) == 0:\n",
    "            return [[]]\n",
    "\n",
    "        dic = {}\n",
    "        for i in strs:\n",
    "            counts = [0] * 26\n",
    "            for ch in i:\n",
    "                counts[ord(ch) - ord('a')] += 1\n",
    "            if tuple(counts) in dic:\n",
    "                dic[tuple(counts)].append(i)\n",
    "            else:\n",
    "                dic[tuple(counts)] = [i]\n",
    "\n",
    "        return list(dic.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        for st in strs:\n",
    "            counts = [0] * 26\n",
    "\n",
    "            for ch in st:\n",
    "                counts[ord(ch)-ord(\"a\")]+=1\n",
    "            \n",
    "            mp[tuple(counts)].append(st)\n",
    "        \n",
    "        return list(mp.values())"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
